<?php

/* Copyright 2010 John Hooley
	  
    This file is part of Portfolio Zen.


    Portfolio Zen is free software: you can redistribute it and/or modify

    it under the terms of the GNU General Public License as published by

    the Free Software Foundation, either version 3 of the License, or

    (at your option) any later version.



    Portfolio Zen is distributed in the hope that it will be useful,

    but WITHOUT ANY WARRANTY; without even the implied warranty of

    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the

    GNU General Public License for more details.



    You should have received a copy of the GNU General Public License

    along with Portfolio Zen.  If not, see <http://www.gnu.org/licenses/>.

 */ 

defined('_JEXEC') or die('Restricted access');

class ZenColor {

  protected $_value;
  protected $_default;

  public function __construct($value, $default){
    $this->_value = $value;
    $this->_default = $default;
  }

  public function __toString(){
    if($this->_isColor($this->_hashFilter($this->_value))) return $this->_hashFilter($this->_value);
    return $this->_default;
  }

  protected function _isColor($value){
    $length = strlen($value);
    if(in_array($length, array(3,4,6,7))){
      return preg_match('|#[A-Za-z0-9][A-Za-z0-9][A-Za-z0-9][A-Za-z0-9]?[A-Za-z0-9]?[A-Za-z0-9]?|', $value);
    }
    return false;
  }

  protected function _hashFilter($value){
    if(substr($value, 0, 1) !== '#') return '#' . $value;
    return $value;
  }
    

}